#include "irvideowidget.h"
#include <QToolTip>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#pragma execution_character_set("utf-8")
IrVideoWidget::IrVideoWidget(QWidget *parent)
    : QWidget{parent}
{
    setMouseTracking(true);  // 启用鼠标追踪
}

void IrVideoWidget::setFrame(const QImage & frame)
{
    QMutexLocker locker(&m_mutex);
    m_currentFrame = QPixmap::fromImage(frame);
    update();
}

void IrVideoWidget::setIrData(const QVector<quint16>& irData, int width, int height)
{
    QMutexLocker locker(&m_mutex);
    m_irData = irData;
    m_irWidth = width;
    m_irHeight = height;
}

void IrVideoWidget::mouseMoveEvent(QMouseEvent* event)
{
    m_lastPos = event->pos();
    QMutexLocker locker(&m_mutex);

    if (m_currentFrame.isNull() || m_irData.isEmpty() || m_irWidth <= 0 || m_irHeight <= 0) {
        QToolTip::hideText();
        return;
    }

    // 计算图像显示区域（同深度图）
    QSize frameSize = m_currentFrame.size();
    QSize widgetSize = size();
    QSize scaledSize = frameSize.scaled(widgetSize, Qt::KeepAspectRatio);
    QRect displayRect(
        (widgetSize.width() - scaledSize.width()) / 2,
        (widgetSize.height() - scaledSize.height()) / 2,
        scaledSize.width(),
        scaledSize.height()
    );

    if (!displayRect.contains(m_lastPos)) {
        QToolTip::hideText();
        return;
    }

    // 转换鼠标坐标到 IR 图像原始像素（同深度图）
    double scaleX = static_cast<double>(m_irWidth) / scaledSize.width();
    double scaleY = static_cast<double>(m_irHeight) / scaledSize.height();
    int imageX = qRound((m_lastPos.x() - displayRect.left()) * scaleX);
    int imageY = qRound((m_lastPos.y() - displayRect.top()) * scaleY);

    if (imageX < 0 || imageX >= m_irWidth || imageY < 0 || imageY >= m_irHeight) {
        QToolTip::hideText();
        return;
    }

    // 获取对应像素的 quint16 值
    int index = imageY * m_irWidth + imageX;
    quint16 irValue = m_irData[index];
    QToolTip::showText(mapToGlobal(m_lastPos),
        QString(tr("坐标: (%1, %2)\nIR 值: %3"))
        .arg(imageX).arg(imageY).arg(irValue),
        this);

    update();
}

void IrVideoWidget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    QMutexLocker locker(&m_mutex);
    if (!m_currentFrame.isNull()) {
        // 居中显示逻辑（同深度图）
        QSize frameSize = m_currentFrame.size();
        QSize widgetSize = size();
        QSize scaledSize = frameSize.scaled(widgetSize, Qt::KeepAspectRatio);
        QRect displayRect(
            (widgetSize.width() - scaledSize.width()) / 2,
            (widgetSize.height() - scaledSize.height()) / 2,
            scaledSize.width(),
            scaledSize.height()
        );

        painter.drawPixmap(displayRect, m_currentFrame);

        // 鼠标在图像上时绘制十字（同深度图）
        /*if (!m_irData.isEmpty() && displayRect.contains(m_lastPos)) {
            QPoint imagePos = m_lastPos - displayRect.topLeft();
            painter.setPen(QPen(Qt::red, 1));
            painter.drawLine(imagePos.x() - 5, imagePos.y(), imagePos.x() + 5, imagePos.y());
            painter.drawLine(imagePos.x(), imagePos.y() - 5, imagePos.x(), imagePos.y() + 5);
        }*/
    }
    else {
        painter.drawText(rect(), Qt::AlignCenter, tr("IR 图像已停止"));
    }
}

void IrVideoWidget::leaveEvent(QEvent* event)
{
    Q_UNUSED(event);
    QToolTip::hideText();
}
